
********************************************************************************
********************************************************************************
** load and set up the dx data
********************************************************************************
********************************************************************************

timer clear 1 
timer on 1
import delimited using health/rdrp3367_dx_de_v5.csv, clear asdouble
timer off 1
timer list 1
describe 

** clean up the text
	replace dx_text = subinstr(dx_text, dx_code, "",.)
	replace dx_text = subinstr(dx_text, "-", " ", .)
	replace dx_text = lower(dx_text)
	replace dx_text = subinstr(dx_text, "(cms/hcc)", "", .)
	replace dx_text = subinstr(dx_text, "athscl", "atherosclerotic", .)
	replace dx_text = subinstr(dx_text, "ang ", "angina ", .)
	replace dx_text = subinstr(dx_text, "pctrs", "pectoris", .)

	replace dx_text = subinstr(dx_text, "hyp ", "hypertensive ", .)
	replace dx_text = subinstr(dx_text, "hrt ", "heart ", .)
	replace dx_text = subinstr(dx_text, "chr ", "chronic ", .)
	replace dx_text = subinstr(dx_text, "kdny ", "disease ", .)
	replace dx_text = subinstr(dx_text, "dis ", "hypertensive ", .)
	replace dx_text = subinstr(dx_text, "fail ", "failure ", .)
	replace dx_text = subinstr(dx_text, "stg ", "stage ", .)
	replace dx_text = subinstr(dx_text, "unsp ", "unspecified ", .)

	replace dx_text = subinstr(dx_text, "w ", "with ", .)
	replace dx_text = subinstr(dx_text, "w/o ", "without ", .)
	replace dx_text = trim(dx_text)

gen dx_period = dx_code
replace dx_code = subinstr(dx_code, ".", "",.)
merge m:1 dx_code using dxccsr/dxccsr, keep(1 3) nogen
gen first3 = substr(def_ccsr_cat, 1, 3)


** merge in ili and cli codes
merge m:1 dx_code using data/ili_cli_codes
tab dx_code if _merge == 2
drop if _merge == 2
replace ili = 0 if _merge == 1
replace cli = 0 if _merge == 1
drop _merge 

egen tencounter_de = tag(encounter_de)
egen tid = tag(sid)

gen c0 = "DXs &" in 1
count
gen c1 = string(`r(N)',"%16.0fc") + "\\"  in 1

replace c0 = "Encounters &" in 2 
count if tencounter_de
replace c1 = string(`r(N)',"%16.0fc") + "\\"  in 2

replace c0 = "Patients &"  in 3
count if tid
replace c1 = string(`r(N)',"%16.0fc") + "\\" in 3
list c0 c1 in 1/3, noobs clean

outsheet c0 c1 in 1/3 using tables/dx_counts.tex, replace ///
	noquote nonames delimit(" ")
drop c0 c1

** any ili/cli across all dx code types
sort encounter_de dx_priority 
by encounter_de: gen tag = _n==1
by encounter_de: egen any_icli_all = max(ili | cli)


** labor or delivery codes (including complications), ever 
	gen dx3 = substr(dx_code, 1, 3)
	gen t_ld = 0
	foreach c of numlist 60/75 80/84 {
		replace t_ld = 1 if dx3 == "O`c'"
	}
	by encounter_de: egen labor_delivery = max(t_ld)
	tab first3 if labor_delivery & dx_priority == 1
	
	
** AMI codes
	gen t_ami = dx3 == "I21" | dx3 == "I22"
	count if t_ami
	by encounter_de: egen ami = max(t_ami)
	
	gen t_repeat_ami = dx3 == "I22" | dx_code == "I252" | dx_code == "I21A1"
	by encounter_de: egen repeat_ami = max(t_repeat_ami)
	tab ami repeat_ami if tag
	gen new_ami = ami & ~repeat_ami

** 	stroke
	gen t_stroke = 0
	foreach c of numlist 61/64 {
		replace t_stroke  = 1 if dx3 == "I`c'"
	}
	by encounter_de: egen stroke = max(t_stroke)
	

** fracture / crush
	gen t_fracture = 0 
	gen t_wound = 0
	forvalues n = 0/9 {
		replace t_fracture = 1 if dx3 == "S`n'2" | dx3 == "S`n'7"
		replace t_wound = 1 if dx3 == "S`n'1"
	}
	* multiple regions
	replace t_fracture = 1 if dx3 == "T02" |  dx3 == "T07"
	replace t_wound = 1 if dx3 == "T01" 
	by encounter_de: egen fracture = max(t_fracture)
	by encounter_de: egen wound = max(t_wound)

	** open wound
	
	
sum any_icli_all labor_delivery ami stroke fracture if tag

** cancers in the admitting or primary diagnosis diagnosis
	gen t_cancer_admit = substr(dx_code, 1,1) == "C" & dx_type_code == "A" 
	by encounter_de: egen cancer_admit = max(t_cancer_admit)
	sum cancer_admit if tag
	gen t_cancer_dx1 =  substr(dx_code, 1,1) == "C" & dx_priority == 1
	by encounter_de: egen cancer_dx1 = max(t_cancer_dx1)
	sum cancer_admit cancer_dx1 if tag
	
	** chemo therapy any dx
	gen t_chemo = 0
	forvalues c = 0/2 {
		replace t_chemo = 1 if substr(dx_code, 1,4) == "Z51`c'"
	}
	by encounter_de: egen chemo = max(t_chemo)
	sum chemo if tag
	sum chemo if tag & ~cancer_admit & ~cancer_dx1
	gen cancer = cancer_admit | cancer_dx1 | chemo

** appendicitis 
	gen t_app = 0
	forvalues c = 35/38 {
		replace t_app = 1 if dx3 == "K`c'"
	}
	by encounter_de: egen appendicitis = max(t_app)
	
** accidents
	** vehicle accident
	by encounter_de: egen vehicle_accident = max(substr(dx_code, 1,1)=="V")
	gen t_other_accident = substr(dx_code,1,1) == "W"
	replace t_other_accident =1 if substr(dx_code, 1,1) == "X" & ///
		real(substr(dx_code,2,2))<=59
	by encounter_de: egen other_accident = max(t_other_accident)
	sum vehicle_accident other_accident if tag
	
	gen any_major = cancer | labor_delivery | ami | stroke | fracture | wound ///
		| vehicle_accident | other_accident | appendicitis
	sum any_icli_all any_major cancer labor_delivery ami stroke fracture wound ///
		appendicitis vehicle_accident other_accident
	
	desc t_*, full
	drop t_*
		
	
** priority: admitting, working final 
by encounter_de: gen n_dx = _N
foreach l in A F W {
	by encounter_de: egen has`l' = max(dx_type_code == "`l'")
}
drop if (dx_type_code == "F" | dx_type_code == "W") & hasA 
drop if (dx_type_code == "W") & hasF
assert dx_type_code ~= "F" & dx_type_code ~= "A" if dx_type_code == "W"

by encounter_de: gen weird = dx_priority[1] ~= 1
count if weird
assert r(N) == 43985
by encounter_de: assert dx_priority > 1 if _n > 1
by encounter_de: assert dx_priority[1] < dx_priority[2]

by encounter_de: egen any_icli = max(ili | cli)  

by encounter_de: keep if _n == 1
label var any_icli "CLI or ILI in any DX"

label var cli "CLI in primary DX"
label var ili "ILI in primary DX"

gen icli_dx1 = ili | cli
label var icli_dx1 "ILI or CLI in primary DX" 

save health/dxs, replace 


